<?php
namespace app\api\controller;

use think\Controller;
use think\Request;
use app\model\QuestionModel;
use app\model\AnswerModel;
use app\model\CompanyModel;
use app\common\BaseController;

class Question extends BaseController
{
    public function _initialize(){
        // $this->request = Request::instance();
        $this->noNeedLogin = [];
    }

    public function add_post(){
        $companyid = trim($this->request->param('companyid'));
        $question = trim($this->request->param('question'));
        $options = trim($this->request->param('options'));
        $answer = trim($this->request->param('answer'));

        if(empty($companyid)){
            return toJson(['message'=>'公司ID不能为空','code'=>400]);
        }

        // if(empty($question) || empty($options) || empty($answer) ){
        //     return toJson(['message'=>'参数不全，请检查','code'=>400]);
        // }

        $questionModel = new QuestionModel;
        $questionModel->companyid = $companyid;
        $questionModel->question = $question;
        $questionModel->options = $options;
        $questionModel->answer = $answer;
        $questionModel->createtime = time();
        $questionModel->save();
        
        if( $questionModel->id > 0 ){
            return toJson(['message'=>'新增问题成功','code'=>'200']);

        }else{
            return toJson(['message'=>'新增问题出错，请联系客服！','code'=>'400']);
        }
    }

    public function edit_post(){
        $id = trim($this->request->param('id'));
        $question = trim($this->request->param('question'));
        $options = trim($this->request->param('options'));
        $answer = trim($this->request->param('answer'));

        if(empty($id)){
            return toJson(['message'=>'问题ID不能为空','code'=>400]);
        }

        if(empty($question) || empty($options) ){
            return toJson(['message'=>'参数不全，请检查','code'=>400]);
        }

        $questionData = QuestionModel::where('id','eq',$id)->find();
        if(empty($questionData)){
            return toJson(['message'=>'问题不存在','code'=>'400']);
        }

        $questionData->question = $question;
        $questionData->options = $options;
        $questionData->answer = $answer;
        $questionData->save();

        return toJson(['message'=>'修改问题成功','code'=>'200']);
    }

    public function delete_post(){
        $id = trim($this->request->param('id'));

        if(empty($id)){
            return toJson(['message'=>'问题ID不能为空','code'=>400]);
        }

        $questionData = QuestionModel::where('id','eq',$id)->find();
        if(!$questionData){
            return toJson(['message'=>'问题不存在','code'=>'400']);
        }

        $questionData->delete();

        return toJson(['message'=>'删除问题成功','code'=>'200']);
    }

    public function getlist_get(){
        $companyid = trim($this->request->param('companyid'));
        $page = trim($this->request->param('page'));
        $pagesize = trim($this->request->param('pagesize'));
        $keyword = trim($this->request->param('keyword'));
        $type =  trim($this->request->param('type')); //train

        $page = empty($page) ? 1 : $page;
        $pagesize = empty($pagesize) ? 10 : $pagesize;

        $where = $keyword ? ":name like %".$keyword."%" : '1=1';
        if($type=='train'){
            // $where = $where.' ORDER BY  RAND() LIMIT 10';
            $questionList = QuestionModel::where($where)->where('companyid','eq',$companyid)->where('deletetime is  null and status = 0')->page($page,$pagesize)->orderRaw("RAND()")->limit(10)->select();
             return toJson(['message'=>'获取问题列表成功','code'=>'200','data'=>$questionList]);
        }
        $questionList = QuestionModel::where('companyid','eq',$companyid)->where($where)->where('deletetime is  null and status = 0')->page($page,$pagesize)->select();

        // $questionList = QuestionModel::where($where)->where('companyid','eq',$companyid)->page($page,$pagesize)->orderRaw("RAND()")->limit(10)->select();
        if(count($questionList)==0){
            return toJson(['code'=>'400','message'=>'None']);
        }
        // $questionList->hidden(['answer']);
        return toJson(['message'=>'获取问题列表成功','code'=>'200','data'=>$questionList]);
    }

    public function train_post(){
        $companyid = trim($this->request->param('companyid'));
        $answer = trim($this->request->param('answer'));
        $type = trim($this->request->param('checktranin'));/*check*/
        
        // if(empty($companyid) || empty($answer)){
        //     return toJson(['message'=>'参数不全，请检查','code'=>400]);
        // }
        
        $companyData = CompanyModel::where('id','eq',$companyid)->find();
        $AnswerData = AnswerModel::where('uid = '.$this->uid.' and companyid = '.$companyid)->order('createtime desc')->find();
        if($type == 'check'){

            if($AnswerData['answered']==0){
                return toJson(['message'=>'您未曾答题，请答题','code'=>'400','status'=>0]);
                }
                return toJson(['message'=>'已完成过答题','code'=>'200','status'=>1]);

        }
        if(empty($companyData)){
            return toJson(['message'=>'数据出错，请联系客服！','code'=>'400']);
        }
        if($answer<$companyData['score']){
            return toJson(['message'=>'未通过培训，请重新答题','code'=>'400']);
        }

        // $count = QuestionModel::where('companyid','eq',$companyid)->count();
        // $answer = json_decode($answer,true);
        // var_dump($count);
        // exit;
        // if($count > count($answer)){
        //     return toJson(['message'=>'未完成培训，请检查','code'=>'400']);
        // }

        // $questionids = [];
        // foreach($answer as $key=>$item ){
        //     $questionids[] = $key;
        // }

        // $rightAnswer = 0;
        // $questionData = QuestionModel::where('id','in',$questionids)->field('id,answer')->select();
        // foreach( $questionData as $item ){
        //     $question = $item->toArray();
        //     if($question['answer'] == $answer[$question['id']] ){
        //         $rightAnswer++;
        //     }
        // }

        // if($rightAnswer < $companyData->score){
        //     return toJson(['message'=>'未通过培训，请重新答题','code'=>'400']);
        // }


        $answerModel = new AnswerModel;
        $answerModel->uid = $this->uid;
        $answerModel->companyid = $companyid;
        $answerModel->answered=1;
        $answerModel->createtime = time();
        $answerModel->expiretime = $companyData->expiretime > 0 ? strtotime('+'.$companyData->expiretime.' month') : 0;
        $answerModel->save();

        // // if( $answerModel->id > 0 ){
        //     $answerModel = new AnswerModel;
        //      $answerModel->answered=1;
        //      $answerModel->save();
            return toJson(['message'=>'恭喜你，通过培训！','code'=>'200']);
            
        // }else{
            // return toJson(['message'=>'培训答题出错，请联系客服！','code'=>'400']);
        // }
    }

}
